package sample.mailinglist;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sample.Constants;
import sample.PMF;
import sample.User;

/**
 * Adds a user to this mailing list
 * @author ago
 *
 */
@SuppressWarnings("serial")
public class DeleteUserServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = request.getParameter("username");
    
    PersistenceManager pm = PMF.get().getPersistenceManager();
    
    String query = "select from " + User.class.getName() + " where username=='" + username + "'";
    List<User> users = (List<User>) pm.newQuery(query).execute();
    try {
      pm.deletePersistent(users);
    } finally {
      pm.close();
    }
    
    request.getRequestDispatcher(Constants.MAILING_LIST).forward(request, response);
  }
}


